<template>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" label-width="auto">
        <el-form-item label="护理等级">
            <el-select v-model="form.levelId" placeholder="请选择等级" style="width: 220px">
                <el-option v-for="item in nursinglevels" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
        </el-form-item>
        <el-form-item label="护理人员">
            <el-select v-model="form.userId" placeholder="请选择人员" style="width: 220px">
                <el-option v-for="item in users" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">保存</el-button>
            <el-button @click="cancleSet">取消</el-button>
        </el-form-item>
    </el-form>
</template>
<script setup name="setlevel">
import { ref, reactive } from "vue";
import { userList, saveOrUpdate } from '@/api/CareManagement/careSetup/index';
import { listInfo } from '@/api/CareManagement/careLevel/index';
import { ElMessageBox, ElNotification } from "element-plus";
import { onMounted } from "vue";

const props = defineProps({
    closeSet: {
        type: Function,
        required: true
    },
    customerId: {
        type: Number,
        required: true
    }
});

const form = ref({
    customerId: props.customerId,
    customerName: '',
    customerAge: '',
    customerSex: '',
    idcard: '',
    roomNo: '',
    buildingNo: '',
    checkinDate: '',
    expirationDate: '',
    contactTel: '',
    bedId: '',
    psychosomaticState: '',
    attention: '',
    birthday: '',
    height: '',
    weight: '',
    bloodType: '',
    filepath: '',
    userId: '',
    levelId: '',
    familyMember:'',
});
const nursinglevels = ref([]);
const users = ref([]);
const cancleSet = (refresh = false) => {
    props.closeSet(refresh); // 确保传递参数 refresh，默认值为 false
};
const onSubmit = () => {
    console.log("upup", form.value); // 修改：将 form 替换为 form.value
    saveOrUpdate(form.value).then((success) => { // 修改：确保传递 form.value
        ElNotification({
            title: "消息",
            type: "success",
            message: "保存成功",
        });
        cancleSet(true); // 确保传递 true 以触发父组件刷新
    });
};
onMounted(async () => {
    try {
        const leveldata = await listInfo();
        const userdata = await userList();
        console.log(leveldata, userdata);
        // 修改：从 data.records 中获取护理等级列表
        if (Array.isArray(leveldata.records) && Array.isArray(userdata.records)) {
            users.value = userdata.records.map(item => ({
                label: item.nickname, // 假设返回的数据中包含 name 字段作为显示名称
                value: item.id   // 假设返回的数据中包含 id 字段作为唯一标识
            }));
            nursinglevels.value = leveldata.records.map(item => ({
                label: item.levelName, // 假设返回的数据中包含 name 字段作为显示名称
                value: item.id   // 假设返回的数据中包含 id 字段作为唯一标识
            }));
        } else {
            console.error("获取护理等级列表失败：返回的数据中 records 不是数组", leveldata, userdata);
        }
    } catch (error) {
        console.error("获取护理等级列表失败", error);
    }
});
</script>

<style scoped>
.demo-form-inline {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 15px 5px;
}

.demo-form-inline .el-input {
    --el-input-width: 500px;
}
</style>